package data.structure.linear.linkedList;

import data.structure.linear.AbstractList;

/**
 * @Author douhy
 * @description 手写双向链表  (java LinkedList底层为双向链表)
 * @date 22/01/17
 */
public class DoubleLinkedList<E> extends AbstractList<E> {

    private DoubleLinkedList.Node<E> first;
    private DoubleLinkedList.Node<E> last;

    // 静态内部类 自实现节点
    private static class Node<E> {
        E element;
        DoubleLinkedList.Node<E> next;
        DoubleLinkedList.Node<E> prev;

        public Node(E element, DoubleLinkedList.Node<E> next, DoubleLinkedList.Node<E> prev) {
            this.element = element;
            this.next = next;
            this.prev = prev;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            if (prev != null) {
                sb.append(prev.element);
            } else {
                sb.append("null");
            }
            sb.append("_").append(element).append("_");

            if (next != null) {
                sb.append(next.element);
            } else {
                sb.append("null");
            }

            return sb.toString();
        }
    }

    @Override
    public void clear() {
        first = null;
        last = null;
        size = 0;
    }

    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = node(index);
        E old = node.element;
        node.element = element;
        return old;

    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);

        // 向链表尾部添加元素
        if (index == size) {
            Node<E> oldLast = last;
            last = new DoubleLinkedList.Node<E>(element, null, oldLast);
            if (oldLast == null) { // 第一次插入
                first = last;
            } else {
                oldLast.next = last;
            }
        } else {
            Node<E> next = node(index);
            Node<E> prev = next.prev;
            Node<E> node = new DoubleLinkedList.Node<E>(element, next, prev);
            next.prev = node;

            if (prev == null) { // index == 0
                first = node;
            } else {
                prev.next = node;
            }
        }
        size++;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);

        Node<E> node = node(index);
        Node<E> prev = node.prev;
        Node<E> next = node.next;

        if (prev == null) { // index == 0
            first = next;
        } else {
            prev.next = next;
        }

        if (next == null) { // index == size - 1
            last = prev;
        } else {
            next.prev = prev;
        }

        size--;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        if (element == null) {
            Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (node.element == null) return i;

                node = node.next;
            }
        } else {
            Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (element.equals(node.element)) return i;

                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }


    private DoubleLinkedList.Node<E> node(int index) {
        rangeCheck(index);

        if (size >> 1 > index) { // 从头节点到中间
            DoubleLinkedList.Node<E> node = first;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        } else { // 从尾结点到中间  (元素数量 - 1 拿到最大索引    最大索引 - 获取元素索引 = 距离    用距离向前拿元素 最终返回)
            DoubleLinkedList.Node<E> node = last;
            for (int i = size - 1; i > index; i--) {
                node = node.prev;
            }
            return node;
        }
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("size=").append(size).append(", [");
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }

            string.append(node);

            node = node.next;
        }
        string.append("]");
        return string.toString();
    }

}
